﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KLS.PPDS.ApplicationService.Manager;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository;
using KLS.PPDS.Utilities;
using KLS.PPDS.Utilities.BaseController;
using KLS.PPDS.Utility;
using KLS.PPDS.Web.Models;

namespace KLS.PPDS.Web.Controllers
{
    /// <summary>
    /// handele all the request from the User screen
    /// </summary>
    public class UserController : BaseController
    {
        //
        // GET: /User/

        private UserViewModel _model;

        /// <summary>
        /// return User main page
        /// </summary>
        /// <returns></returns>
        [AjaxAuthorize(Roles =  RoleConstant.SuperadminOrEditor)]
        public ActionResult Index(UserViewModel _model, int? SrcCompanyId)
        {

            
            _model.UserList = UserManager.GetAllByComapanyId(SrcCompanyId);//UserRepository.Instance().FindAll().ToList();
            _model.CompanyList = new SelectList(CompanyManager.GetAllCompany(),"Id","CompanyName",CommonSession.CompanyId);
            
            return View(_model);
        }

        /// <summary>
        /// Get the list
        /// </summary>
        /// <param name="model"></param>
        /// <param name="SrcCompanyId"></param>
        /// <returns></returns>

         [AjaxAuthorize(Roles = RoleConstant.SuperadminOrEditor)]
        public ActionResult UserList(UserViewModel model ,int? SrcCompanyId)
        {
            model.UserList = UserManager.GetAllByComapanyId(SrcCompanyId);
            return PartialView(model);
        }

        /// <summary>
        /// return the UserSetting screen,used for create new user
        /// </summary>
        /// <returns>UserSettings screen</returns>
        [HttpGet]
        [AjaxAuthorize(Roles = RoleConstant.SuperadminOrEditor)]
        public ActionResult UserSettings(UserViewModel _model, int? SrcCompanyId)
        {
            
            _model.User = new User();

            //if user admin
            if (CommonSession.IsUserSuperAdmin)
            {
                _model.SrcCompanyId = SrcCompanyId.GetValueOrDefault(0);
                _model.User.CompanyId = _model.SrcCompanyId;

                _model.CompanyList = new SelectList(CompanyManager.GetAllCompany(), "Id", "CompanyName");
               
                _model.RoleList = new SelectList(RoleManager.GetAll(), "Id", "RoleName");
            }

            //if user editor
            if(CommonSession.IsUserEditor)
            {
                _model.User.RoleId = RoleManager.Get(CommonConstant.Journalist).Id;
                _model.User.CompanyId = CommonSession.CompanyId;
            }

            _model.LanguageList = new SelectList(LanguageManager.GetAll(), "Id", "Name");

            return PartialView(_model);
        }

        /// <summary>
        /// Save or Update an User
        /// </summary>
        /// <param name="model">UserViewModel</param>
        /// <returns>index/userlist screen</returns>
        [HttpPost]
        public ActionResult SaveUserSettings(UserViewModel model)
        {
            var user = model.User.Id==0 ? new Core.Entity.User() : UserManager.Get(model.User.Id);

            user.UserName = model.User.UserName;
            user.Password = model.User.Password;
            user.PhoneNo = model.User.PhoneNo;
            user.CompanyId = model.User.CompanyId;
            user.RoleId = model.User.RoleId;
            user.LanguageId = model.User.LanguageId;
            user.CreatedDate = DateTime.Now;
            user.UserFullName = model.User.UserFullName;

            var msg = UserManager.Validate(user);

            if (String.IsNullOrEmpty(msg)) 
            {
                UserManager.Save(user);
                return RedirectToAction("UserList", new { @SrcCompanyId = model.SrcCompanyId });
            }

            return JsonMessage(msg);

        }

        /// <summary>
        /// return the UserSettings screen
        /// </summary>
        /// <param name="userId">userid that need to be edit</param>
        /// <returns></returns>
        
        [AjaxAuthorize(Roles = RoleConstant.SuperadminOrEditor)]
        public ActionResult EditUser(string userId)
        {
            _model = new UserViewModel();

            _model.User = UserManager.Get(Convert.ToInt32(userId));
            _model.CompanyList = new SelectList(CompanyManager.GetAllCompany(), "Id", "CompanyName");
            _model.LanguageList = new SelectList(LanguageManager.GetAll(), "Id", "Name");
            _model.RoleList = new SelectList(RoleManager.GetAll(), "Id", "RoleName");
            return PartialView("~/Views/User/UserSettings.ascx", _model);

        }

        /// <summary>
        /// Delete a User from Database by setting the IsDeleted flag ture
        /// </summary>
        /// <param name="userId">UserId that need to delete</param>
        /// <returns></returns>
         [AjaxAuthorize(Roles = RoleConstant.SuperadminOrEditor)]
        public ActionResult DeleteUser(string userId, int? srcCompanyId)
        {

          UserManager.Delete(int.Parse(userId));

          ModelState.Clear();
          var model = new UserViewModel();
          model.UserList = UserManager.GetAllByComapanyId(srcCompanyId);
          return PartialView("~/Views/User/UserList.ascx", model);

        }


        



        

    }
}
